package test;


import data.StringDataSetGenerator;
import implementation.search.BinaryOrderedArray;
import implementation.search.InterpolateOrderedArray;
import implementation.search.TextIndex;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class TextIndexReport {

    public static void main(String[] args) {
        BinaryOrderedArray binaryA = new BinaryOrderedArray();
        InterpolateOrderedArray interpolateA = new InterpolateOrderedArray();

        TextIndex textIndexBin = new TextIndex(binaryA);
        TextIndex textIndexInt = new TextIndex(interpolateA);
        String text = getText("src/test/moby_dick.txt");

        StringDataSetGenerator generator = new StringDataSetGenerator();
        List<String> wordList;
        ArrayList<Integer> nList = getNList();
        int n = 0;

        for (int i = 0; i < nList.size(); i++) {
            n = nList.get(i);

            textIndexBin.buildIndex(text, n);
//            textIndexInt.buildIndex(text, n);

            wordList = generator.createFromFile("src/test/moby_dick.txt", n);
            System.out.println("N= " + n);
            System.out.println("\tBinary:");


            printReport(textIndexBin, n, wordList);
            //System.out.println("\tInterpolate:");
            //printReport(textIndexInt, n, wordList);
        }
    }

    private static void printReport(TextIndex textIndex, int n, List<String> wordList) {

        long t0 = System.currentTimeMillis();
        for (int j = 0; j < n; j++) {
            textIndex.search(wordList.get(j));
        }
        long tf = System.currentTimeMillis();
        System.out.println("\t\tTime: " + (tf - t0));
    }

    public static ArrayList<Integer> getNList() {
        ArrayList<Integer> nList = new ArrayList<Integer>();
        nList.add(5000);
        nList.add(50000);
        nList.add(100000);
        nList.add(150000);
        nList.add(200000);
        return nList;
    }

    public static String getText(String fileName) {
        String text = "";
        try {
            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);
            String s = br.readLine();
            int n = 50000;
            while (s != null && n > 0) {
                text += s;
                s = br.readLine();
                n--;
            }
            fr.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return text;
    }
}




